package com.atwy.linearstructure.linkedList;

/**
 * @Author: 小王子火
 * @Date: 2020/12/4
 * <p>
 * 双向链表的案例
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {

    }
}

/**
 * 双向链表
 * 1、增
 * 2、删
 * 3、改
 * 4、查
 *
 * 根据前一个可查后一个，根据后一个可查前一个
 * 基本和单向链表差不多
 *
 */
class DoubleLinkedList {
    /**
     * 头节点
     */
    private DoubleNode headNode = new DoubleNode(0, "", "");

    /**
     * 增
     *
     * @param newNode
     */
    public void add(DoubleNode newNode) {
        DoubleNode temp = headNode;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.pre = temp;
    }

    /**
     * 按英雄排名删除，
     * 先遍历找到节点
     *
     * @param hernNo
     */
    public void delete(int hernNo) {
        DoubleNode temp = headNode.next;
        while (temp != null) {
            if (hernNo == temp.heroNo) {
                // 找到了
                temp.pre.next = temp.next;
                if(temp.next != null){
                    temp.next.pre = temp.pre;
                }
                break;
            }
            temp = temp.next;
        }
    }



}

/**
 * 节点
 */
class DoubleNode {
    /**
     * data域
     */
    int heroNo;
    String name;
    String nickName;

    /**
     * pre域
     */
    DoubleNode pre;

    /**
     * next域
     */
    DoubleNode next;

    public DoubleNode(int heroNo, String name, String nickName) {
        this.heroNo = heroNo;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DoubleNode{");
        sb.append("heroNo=").append(heroNo);
        sb.append(", name='").append(name).append('\'');
        sb.append(", nickName='").append(nickName).append('\'');
        sb.append('}');
        return sb.toString();
    }
}